<template>
  <div class="header">
    <div class="inner rowBetweenNoWarp">
      <a href="" class="logo-name">
        <h3 class="title">{{ wpzswTitle }}</h3>
      </a>
      <a
        href="javascript:void(0);"
        class="btn-list"
        :class="{ 'btn-close': isLoginInfo }"
        @click="showInfo"
        v-if="!isIndex"
      >
        <van-icon
          class="iconfont icongerenzhongxinxuqiushuomingshuv100822"
          name="dian"
        ></van-icon>
      </a>
      <van-popup v-model="isLoginInfo" position="top" v-if="!isIndex">
        <div class="login-info">
          <!-- <router-link class="index-link" :to="{ path: '/' }">首页</router-link> -->
          <a href="javascript:void(0);" class="index-link" @click="goIndex"
            >首页</a
          >
          <div class="login-over" v-if="userName && !isShow">
            <span>你好，{{ userName }}</span>
            <div>
              <a href="javascript:void(0);" @click="logout">注销</a>
              <a href="javascript:void(0);" @click="signout">退出</a>
            </div>
          </div>
        </div>
      </van-popup>
    </div>
  </div>
</template>

<script>
import { Dialog } from "vant";
import localDb from "../utils/localDb";
export default {
  name: "Header",
  props: {
    isShow: {
      type: Boolean,
      default: true,
    },
    isIndex: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      isLoginInfo: false,
    };
  },
  mounted() {
    this.isLoginInfo = false;
  },
  computed: {
    userName() {
      return this.$store.state.user;
    },
    wpzswTitle() {
      return this.$store.state.wpzswTitle;
    },
    isDelayApply() {
      return this.$store.state.isDelayApply;
    },
    verify() {
      return this.$store.state.verify;
    },
  },
  methods: {
    goIndex() {
      this.$router.push("/");
      this.isLoginInfo = !this.isLoginInfo;
    },
    showInfo() {
      this.isLoginInfo = !this.isLoginInfo;
    },
    signout() {
      Dialog.confirm({
        title: "提示",
        message: "确定要退出账号登录吗？",
      })
        .then(() => {
          this.isLoginInfo = !this.isLoginInfo;
          this.$store.commit("SETUSER", "");
          this.$store.commit("SETTOKEN", "");
          this.$store.commit("SETSTULIST", "");
          this.$store.commit("SETSTVERIFYT", "");
          this.$router.push({ path: "/zmkm" });
        })
        .catch(() => {});
    },
    logout() {
      if (this.verify.res == -1) {
        this.$toast(this.verify.msg);
        this.isLoginInfo = false;
        return false;
      }
      Dialog.confirm({
        title: "提示",
        message:
          "确定要注销账户吗？注销后将删除你的账号（包括基本信息、图片、报名记录），账号也无法登录，但可以重新注册。（注销点击“确定”，不注销点击“取消”）",
      })
        .then(() => {
          if (this.isDelayApply) {
            this.$fetch("/api/Student/PersonDelayLogOut", {}, "post").then(
              (data) => {
                if (data.res == 0) {
                  // this.isLoginInfo = false;
                  this.$router.push({ path: "/" });
                } else {
                  this.$toast(data.msg);
                }
              }
            );
          } else {
            this.$fetch("/api/Student/LogOut", {}, "post").then((data) => {
              if (data.res == 0) {
                this.$router.push({ path: "/" });
              } else {
                this.$toast(data.msg);
              }
            });
          }
          this.isLoginInfo = !this.isLoginInfo;
        })
        .catch(() => {});
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.header {
  background: #fff;
  .inner {
    height: 90px;
    box-shadow: 0 2px 6px 0 rgba(0, 0, 0, 0.1);
  }
  h3 {
    font-size: 36px;
  }
  .logo-name {
    display: block;
    line-height: 88px;
    color: #000;
    font-size: 36px;
    text-decoration: none;
  }
  .btn-list {
    position: absolute;
    top: 28px;
    right: 27px;
    width: 36px;
    height: 32px;
    .iconfont {
      display: inline-block;
      font-size: 42px;
      vertical-align: top;
      color: #333;
    }
  }
  .login-info {
    padding-bottom: 3px;
    line-height: 45px;
    background: #fff;
    font-size: 32px;
    text-align: center;
    border-top: 2px #ebebeb solid;
    a {
      padding: 21px 0;
      color: $bsC;
      display: block;
    }
  }
  .login-before {
    a {
      color: #333;
    }
  }
  .login-over {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    div {
      // display: flex;
      width: 100%;
      a {
        color: #333;
        &:first-child {
          margin-top: 20px;
          border-top: 1px solid #ebebeb;
          border-bottom: 1px solid #ebebeb;
        }
      }
    }
  }
}
</style>
<style lang="scss">
.header .van-popup--top {
  top: 88px;
}
.header .van-overlay {
  top: 88px;
}
</style>
